import request from '@/utils/request'

/*
获取cookie
*/
export function getJisiluCookie() {
	return request({
		url: '/ajax/get-cookie',
		method: 'post',
        data: {
            url: 'https://www.jisilu.cn/',
            header: [
                {key: 'User-Agent', val: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55'}
            ]
        }
	})
}

export function getJisiluCookieByCache() {
    let _cacheKey = 'www-jisilu-cookies'
    let _cache = sessionStorage.getItem(_cacheKey)
    if (_cache) {
        console.log('getJisiluCookie...cache', _cache)
        return _cache
    }

    _cache = ''
    getJisiluCookie().then((res) => {
        console.log('getJisiluCookie...start')
        let _jisiluCookies = res.data || []
        if (_jisiluCookies.length > 0) {
            _jisiluCookies.forEach(row => {
                if (row.Name && row.Value) {
                    if (_cache) {
                        _cache += '; '
                    }
                    _cache += row.Name + '=' + row.Value
                }
            })
        }

        sessionStorage.setItem(_cacheKey, _cache)
        console.log('getJisiluCookie...finish', _cache)
        return getJisiluCookieByCache()
    })
}

/*
可转债指数
https://www.jisilu.cn/data/cbnew/cb_index/
*/
export function getJisiluCbIndexHtml() {
	return request({
		url: '/ajax/get-html',
		method: 'post',
        data: {
            url: 'https://www.jisilu.cn/data/cbnew/cb_index/',
            header: [
                {key: 'Referer', val: 'https://www.jisilu.cn'},
                {key: 'User-Agent', val: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55'},
                // {key: 'cookie', val: getJisiluCookieByCache()}
            ]
        }
	})
}

//从可转债指数的网页中，解析JSON数据
export function praseJisiluCbIndexHtml(html) {
    let _start = html.indexOf('var __date')
    let _end = html.indexOf('var maxIndex')
    let _data = html.slice(_start, _end)
    _data = _data.replace(/\t/g, '')
    _data = _data.replace(/\n/g, '')
    _data = _data.replace(/ /g, '')
    // _data = _data.replace(/var__/g, 'var __')

    _data = _data.replace(/=/g, '')
    _data = _data.replace(/;/g, '')
    _data = _data.replace(/"/g, '')
    _data = _data.replace(/var__date/g, '')

    _data = _data.replace(/\[,/g, '[0,')
    _data = _data.replace(/,]/g, ',0]')

    while (true) {
        if (_data.indexOf(',,') <= 0) {
            break
        }
        _data = _data.replace(/,,/, ',0,')
    }

    let _ret = _data.split('var__data')
    // debugger
    if (_ret.length == 2) {
        return {
            '__date': JSON.parse(_ret[0].replace(/'/g, '"')),
            '__data': JSON.parse(_ret[1].replace(/'/g, '"'))
        }
    }

    return {
        '__date': [],
        '__data': {}
    }
}


/*
封闭基金
https://www.jisilu.cn/data/cf/cf_list/?___jsl=LST___t=1642730741377
*/
export function getJisiluCfList() {
	return request({
		url: '/ajax/do-post',
		method: 'post',
        data: {
            url: 'https://www.jisilu.cn/data/cf/cf_list/?___jsl=LST___t=' + (new Date() * 1),
            param: {
                rp: 25,
                page: 1
            },
            header: [
                {key: 'Referer', val: 'https://www.jisilu.cn'},
                {key: 'User-Agent', val: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55'},
                // {key: 'cookie', val: getJisiluCookieByCache()}
            ]
        }
	})
}

/*
获取指数行情
https://www.jisilu.cn/data/idx_performance/list/?___jsl=LST___t=1642737525984
*/
export function getJisiluIdxPerformanceList() {
	return request({
		url: '/ajax/do-post',
		method: 'post',
        data: {
            url: 'https://www.jisilu.cn/data/idx_performance/list/?___jsl=LST___t=' + (new Date() * 1),
            param: {
                rp: 25,
                page: 1
            },
            header: [
                {key: 'Referer', val: 'https://www.jisilu.cn'},
                {key: 'User-Agent', val: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55'},
                // {key: 'cookie', val: getJisiluCookieByCache()}
            ]
        }
	})
}
